//
//  HomeRoomVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/24.
//  Copyright © 2020 tym. All rights reserved.
//

#import "HomeRoomVC.h"
#import "HomeRoomCell.h"
#import "RoomManagerSunVC.h"
#import "CustomGifHeader.h"
#import "TYMBaseTableView.h"
#import "HomeRoomDetailVC.h"
@interface HomeRoomVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) TYMBaseTableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) FamilyRoomService *roomService;
@end

@implementation HomeRoomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initTableView];
    TYMWeak(self);
    self.tableView.mj_header = [CustomGifHeader headerWithRefreshingBlock:^{
        [weakself getFamilyRoomsData];
    }];
    
}


-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)initTableView{
    
    TYMBaseTableView *tb = [[TYMBaseTableView alloc] initWithFrame:CGRectZero];
    [tb registerNib:[UINib nibWithNibName:@"HomeRoomCell" bundle:nil] forCellReuseIdentifier:[HomeRoomCell identifier]];
    tb.delegate = self;
    tb.dataSource = self;
    tb.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tb];
    self.tableView = tb;
    
    [tb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.view);
    }];
}

-(void)getFamilyRoomsData{
    TYMWeak(self);
    [self.roomService getFindfamilyroomequipment :[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull response, NSArray * _Nonnull roosArry) {
        [weakself.tableView.mj_header endRefreshing];
        if (weakself.dataArray.count) {
            [weakself.dataArray removeAllObjects];
            [weakself.tableView reloadData];
        }
        [weakself.dataArray addObjectsFromArray:roosArry];
        if (weakself.homeRootCountBlock) {
            weakself.homeRootCountBlock(weakself.dataArray.count);
        }
        [weakself.tableView reloadData];
    }];
}

#pragma  mark - tableView 代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [HomeRoomCell cellForHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeRoomCell identifier] forIndexPath:indexPath];
    [cell setRoomModel:self.dataArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RoomModel *roomModel = self.dataArray[indexPath.row];
    
    HomeRoomDetailVC *detailVC = [[HomeRoomDetailVC alloc] initWithRoomModel:roomModel];
    [self.navigationController pushViewController:detailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
